﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;
using GraphDrawer.GraphProvider;

namespace GraphDrawer.Reports.Charts
{
    public partial class SimulationSimpleReport : UserControl
    {
        private GraphProvider.GraphProviderServiceSoapClient cl;
        public Chart chart;

        public SimulationSimpleReport(GraphProvider.GraphProviderServiceSoapClient cl)
        {
            InitializeComponent();
            this.cl = cl;
            cl.getAsyncSimulationReportCompleted += new EventHandler<GraphDrawer.GraphProvider.getAsyncSimulationReportCompletedEventArgs>(cl_getAsyncSimulationReportCompleted);
            LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
        }

        public void Init()
        {
            cl.getAsyncSimulationReportAsync();
        }

        void cl_getAsyncSimulationReportCompleted(object sender, GraphDrawer.GraphProvider.getAsyncSimulationReportCompletedEventArgs e)
        {
            GraphDrawer.GraphProvider.SimulationSimpleReport report = e.Result;

            simTime.Text = report.simTime.ToString();

            chart = new Chart();
            chart.Width = LayoutRoot.Width;
            chart.Height = LayoutRoot.Height;
            chart.Background = new SolidColorBrush(Colors.Gray);

            DataSeries dataSeries = new DataSeries();
            dataSeries.RenderAs = RenderAs.Column;

            foreach (var dataSet in report.dataSets)
            {
                DataPoint dataPoint = new DataPoint();
                dataPoint.AxisXLabel = dataSet.label;
                dataPoint.Color = new SolidColorBrush((Color)(typeof(Colors)).GetProperty(dataSet.color).GetValue(null, null));
                dataPoint.YValue = dataSet.value;
                dataSeries.DataPoints.Add(dataPoint);
            }
            chart.Series.Add(dataSeries);
            LayoutRoot.Children.Add(chart);
        }

        void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (chart != null)
            {
                chart.Width = e.NewSize.Width;
                chart.Height = e.NewSize.Height;
            }
        }
    }
}
